<?php
/**
 * Created by PhpStorm.
 * User: liuxiaojie
 * Date: 2019-03-02
 * Time: 12:28
 */

namespace app\api\controller;

use app\common\BaseLoginCompany;
use app\exception\CustomException;
use app\model\JobWelfare;
use app\tools\MIniprogram;
use think\Db;
use think\Request;

class Firm extends BaseLoginCompany
{

    function __construct(Request $request = null)
    {
        parent::__construct($request);
    }

    // 企业认证

    function auth(){

        $mobile = $this->getParam('mobile', '');
        $code = $this->getParam('code', 0);
        $name = $this->getParam('name', '');
        $brief = $this->getParam('brief', '');
        $address = $this->getParam('address', '');
        $lat = $this->getParam('lat', 0);
        $lng = $this->getParam('lng', 0);
        $tel = $this->getParam('tel', '');
        $face_img = $this->getParam('face_img', '');
        $overleaf_img = $this->getParam('overleaf_img', '');
        $licence_img = $this->getParam('licence_img', '');
        $form_id = $this->getParam('form_id', '');
        $company_id = $this->getParam('u_id', 0);
        $openid = $this->getParam('openid', '');
        $type = $this->getParam('type', '');

        if(!$mobile || !$name || !$brief || !$address || !$tel || !$face_img || !$overleaf_img || !$licence_img || !$type){
            exit(fail("REQUIRE"));
        }

        if($form_id){
            db('form_data')->insert([
                'form_id'=> $form_id,
                'openid'=> $openid,
                'company_id'=> $company_id,
                'create_time'=> time(),
            ]);
        }

        \app\tools\Sms::checkSms($mobile, $code);

        $data = [
            "name"=> $name,
            "mobile"=> $mobile,
            "brief"=> $brief,
            "address"=> $address,
            "lat"=> $lat,
            "lng"=> $lng,
            "tel"=> $tel,
            "face_img"=> $face_img,
            "overleaf_img"=> $overleaf_img,
            "licence_img"=> $licence_img,
            "status"=> 2,
            "is_send"=> 0,
            "type"=> $type
        ];

        db('company')->where(['id'=> $company_id])->update($data);

        return success([]);



    }

    // 企业信息

    function info(){

        $u_id = $this->getParam('u_id', 0);

        $where = [
            'id'=> $u_id
        ];

        $data = db('company')->field('name, brief, address, mobile, tel, status, lat, lng, qrcode')->where($where)->find();
        $info = \db('my')->where(['id'=> 1])->field('content')->find();
//        $data['qrcode'] = $info['qrcode'];
        $data['content'] = $info['content'];

        return success($data);

    }

    function edit(){
        $u_id = $this->getParam('u_id', 0);
        $address = $this->getParam('address', '');
        $lat = $this->getParam('lat', 0);
        $lng = $this->getParam('lng', 0);
        $tel = $this->getParam('tel', '');
        $mobile = $this->getParam('mobile', '');
        $code = $this->getParam('code', 0);

        if(!$address || !$mobile || !$tel){
            exit(fail("DATA_ERROR"));
        }

        \app\tools\Sms::checkSms($mobile, $code);

        $where = [
            'id'=> $u_id
        ];

        $post = [];

        if($address){
            $post['address'] = $address;
            $post['lat'] = $lat;
            $post['lng'] = $lng;
        }

        if($mobile){
            $post['mobile'] = $mobile;
        }

        if($tel){
            $post['tel'] = $tel;
        }

        db('company')->where($where)->update($post);

        return success([]);
    }

    // 招工

    function recruit(){

        $cate_id = $this->getParam('cate_id', 0);
        $cate = $this->getParam('cate', '');
        $num = $this->getParam('num', 0);
        $pay_id = $this->getParam('pay_id', 0);
        $pay = $this->getParam('pay', '');
        $address = $this->getParam('address', '');
        $lat = $this->getParam('lat', 0);
        $lng = $this->getParam('lng', 0);
        $w_id = $this->getParam('w_id', '');
        $detail = $this->getParam('detail', '');
        $mobile = $this->getParam('mobile', '');
        $form_id = $this->getParam('form_id', '');
        $company_id = $this->getParam('u_id', 0);
        $job_id = $this->getParam('job_id', 0);
        $openid = $this->getParam('openid', '');
        $area_tag = $this->getParam('area_tag', 0);


        if(!($cate || $cate_id) || !$num || !($pay || $pay_id) || !$address || !$detail || !$company_id || !$area_tag){
            exit(fail("REQUIRE"));
        }

        $status = \db('company')->where(['id'=> $company_id])->value('status');

        if($status != 3){
            exit(fail("UNAUTHORIZED_OPERATION"));
        }

        if($form_id){
            db('form_data')->insert([
                'form_id'=> $form_id,
                'openid'=> $openid,
                'company_id'=> $company_id,
                'create_time'=> time(),
            ]);
        }


        Db::startTrans();
        try{
            if($cate_id){
                $cate = \db('job_category')->where(['id'=> $cate_id])->value('name');
            }else{
                $cate_id = db('job_category')->where(['name'=> $cate])->value('id');
                if(!$cate_id){
                    $cate_id = \db('job_category')->insertGetId(['name'=> $cate, 'status'=> 0]);
                }
            }
            if($pay){

                if(strrpos($pay, '-') !== false){
                    $arr = explode('-', $pay);

                    $min = $arr[0];
                    $max = $arr[1];

                }else{
                    $dd = $this->str2num($pay);
                    $min = $dd['min'];
                    $max = $dd['max'];
                }

            }else{
                $pay_info = \db('job_pay')->where(['id'=> $pay_id])->field('name, min, max')->find();
                $pay = $pay_info['name'];
                $min = $pay_info['min'];
                $max = $pay_info['max'];
            }

            $post = [
                "name"=> $cate.$num."人",
                "pay"=> $pay.'/月',
                "min"=> $min,
                "max"=> $max,
                "address"=> $address,
                "lat"=> $lat,
                "lng"=> $lng,
                "cate_id"=> $cate_id,
                "p_id"=> 0,
                "c_id"=> 0,
                "county_id"=> 0,
                "company_id"=> $company_id,
                "num"=> $num,
                "mobile"=> $mobile,
                "detail"=> $detail,
                "status"=> 1,
                "is_send"=> 0,
                "pay_id"=> $pay_id?:'',
                "area_tag"=> $area_tag,
                "publish_time"=> date('Y-m-d H:i:s'),

            ];

            if($job_id){

                $count = \db('job')->where(['id'=> $job_id, 'company_id'=> $company_id])->count();
                if(!$count){
                    Db::rollback();
                    return fail("UNAUTHORIZED_OPERATION");
                }

                db('job')->where(['id'=> $job_id])->update($post);
                db('job_tag')->where(['job_id'=> $job_id])->delete();

            }else{

                $job_id = \db('job')->insertGetId($post);
            }

            $tag = [];
            if($w_id) $w_id_arr  =  explode(',', $w_id);
            foreach ($w_id_arr as $v){
                $tag[] = [
                    'job_id'=> $job_id,
                    'w_id'=> $v
                ];
            }

            if($tag) \db('job_tag')->insertAll($tag);

            Db::commit();


        }catch (\Exception $exception){
            Db::rollback();
//            echo $exception->getMessage();
            return fail();
        }

        return success([]);



    }

    // 删除招工

    function deleteJob(){

        $job_id = $this->getParam('job_id', 0);
        $company_id = $this->getParam('u_id', 0);

        $count = \db('job')->where(['id'=> $job_id, 'company_id'=> $company_id])->count();
        if(!$count){
            return fail("UNAUTHORIZED_OPERATION");
        }

        db('job')->where(['id'=> $job_id])->delete();
        return success([]);

    }

    // 福利

    function welfare(){
        $data = JobWelfare::allData();
        return success($data);
    }

    //
    function allJob(){

        $u_id = $this->getParam('u_id', 0);
        $page = $this->getParam('page', 1);
        $page_size = $this->getParam('page_size', 10);

        $where = [
            'company_id'=> $u_id,
        ];

        $list = \db('job')->where($where)->field('name, pay, publish_time, address, status, id')->page($page, $page_size)->select();
        foreach ($list as &$v){

            $tag = db('job_tag')->where(['job_id'=> $v['id']])->column('w_id');
            $v['tag'] = $tag;
            $v['publish_time'] = date('Y-m-d H:i', strtotime($v['publish_time']));
        }

        $welfare = JobWelfare::allData();

        $result = [
            'list'=> $list,
            'welfare'=> array_column($welfare, 'name', 'id')
        ];

        return success($result);

    }

    function putJob(){

        $job_id = $this->getParam('job_id', 0);
        $u_id = $this->getParam('u_id', 0);

        $form_id = $this->getParam('form_id', '');
        $openid = $this->getParam('openid', '');

        $count = \db('job')->where(['company_id'=> $u_id, 'id'=> $job_id])->count();
        if(!$count){
            return fail("UNAUTHORIZED_OPERATION");
        }

        if($form_id){
            db('form_data')->insert([
                'form_id'=> $form_id,
                'openid'=> $openid,
                'company_id'=> $u_id,
                'create_time'=> time(),
            ]);
        }

        \db('job')->where(['id'=> $job_id])->update(['status'=> 1, 'is_send'=> 0]);
        return success([]);

    }

    function pullJob(){

        $job_id = $this->getParam('job_id', 0);
        $u_id = $this->getParam('u_id', 0);

        $count = \db('job')->where(['company_id'=> $u_id, 'id'=> $job_id])->count();
        if(!$count){
            return fail("UNAUTHORIZED_OPERATION");
        }

        \db('job')->where(['id'=> $job_id])->update(['status'=> 3]);
        return success([]);

    }

    function str2num($str){

        $max = $min = 0;

        if(strrpos($str, '以上') !== false){

            $min = str2num(str_replace('以上', '', $str));
            $max = 99999999;

        }elseif (strrpos($str, '以下') !== false){

            $max = str2num(str_replace('以下', '', $str));
            $min = 0;

        }

        return [
            'min'=> $min,
            'max'=> $max
        ];

    }
}